package anc.tankshooter.ui;

import java.util.ArrayList;

import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import anc.tankshooter.GameActivity;
import anc.tankshooter.GameSetting;
import anc.tankshooter.ui.SceneManager.SceneType;

public class PickLevelScreen extends IScreen {

	private Sprite mBackgroundSprite;
	private Sprite mLogoGame;
	private TiledSprite mLeveTitle;
	private Sprite mBoxPanel;
	private ArrayList<ButtonSprite> mButtonLevels;
	@Override
	protected void createScene() {
		mButtonLevels=new ArrayList<ButtonSprite>();
		
		mBackgroundSprite = new Sprite(0, 0, ResourcesManager.getInstance().mBackgroundTexture,mVertextManager);
		mLogoGame= new Sprite(205,50,mResourcesManager.mLogoColorTexture, mVertextManager);
		mLeveTitle= new TiledSprite(200,180,mResourcesManager.mDialogTitlesTexture, mVertextManager);
		mLeveTitle.setCurrentTileIndex(2);
		
		mBoxPanel = new Sprite(207, 180, ResourcesManager.getInstance().mPanelTexture, mVertextManager);
		
		for (int i =1; i <= GameSetting.MAX_LEVEL; i++) {
			
			float x = 230+(i-1)*ResourcesManager.getInstance().mLevelBoxTexture.getWidth()+20*(i-1);
			float y = 300;
			ButtonSprite sprite = new ButtonSprite(x, y,ResourcesManager.getInstance().mLevelBoxTexture, mVertextManager){
				@Override
				public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
						float pTouchAreaLocalX, float pTouchAreaLocalY) {
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
						int level = Integer.valueOf(this.getTag());
						if(level<=GameActivity.mGlobalSetting.getUnlockLevel())
							onLevelClicked(level);
					}
					return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
				}
			};
			
			if(i<=GameActivity.mGlobalSetting.getUnlockLevel()) {
				sprite.setCurrentTileIndex(0);
				Text text = new Text(0, 0, ResourcesManager.getInstance().mWhiteFont20, i+"", 2, mVertextManager);
				text.setX(sprite.getWidth()/2-text.getWidth()/2);
				text.setY(sprite.getHeight()/2-text.getHeight()/2);
				text.setColor(Color.WHITE);
				sprite.attachChild(text);
				registerTouchArea(sprite);
			}
			else sprite.setCurrentTileIndex(1);
			sprite.setTag(i);
			mButtonLevels.add(sprite);
			
		}
		
		attachChild(mBackgroundSprite);
		attachChild(mLogoGame);
		attachChild(mBoxPanel);
		attachChild(mLeveTitle);
		for(ButtonSprite bs:mButtonLevels){
			attachChild(bs);
		}
	}

	private void onLevelClicked(int level){
		if(GameActivity.loadLevelSetting(level)) {
			ResourcesManager.getInstance().loadGameResources();
			GameScreen screen = new GameScreen();
			SceneManager.getInstance().setScene(screen);
		}
	}
	
	

	@Override
	public boolean onBackKeyPressed() {
		ResourcesManager.getInstance().loadMenuResources();
		MainMenuScreen screen = new MainMenuScreen();
		SceneManager.getInstance().setScene(screen);
		return true;
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_SELECTLEVEL;
	}

	@Override
	public void disposeScene() {
	}

	@Override
	public void onUpdateScreen(float second) {
		// TODO Auto-generated method stub
		
	}

}
